package com.xmut.rc.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmut.rc.common.Result;
import com.xmut.rc.entity.CarInfo;
import com.xmut.rc.service.CarInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 车辆信息表 前端控制器
 * </p>
 *
 * @author 小宋
 * @since 2022-06-25
 */

@Slf4j
@RestController
@RequestMapping("/rc/car-info")
public class CarInfoController {

    @Autowired
    private CarInfoService carInfoService;

    /**
     * 分页列表查询
     *
     * @param carInfo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping("/list")
    public Result<?> queryPageList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            CarInfo carInfo) {
        Page<CarInfo> pageParam = new Page<>(pageNo, pageSize);
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.hasText(carInfo.getUserId())){
            queryWrapper.eq("user_id",carInfo.getUserId());
        }
        IPage<CarInfo> pageList = carInfoService.page(pageParam, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping("/adminList")
    public Result<?> adminList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            CarInfo carInfo) {
        Page<CarInfo> pageParam = new Page<>(pageNo, pageSize);
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        IPage<CarInfo> pageList = carInfoService.page(pageParam, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param carInfo
     * @return
     */
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CarInfo carInfo) {
        carInfoService.save(carInfo);
        return Result.OK("添加成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CarInfo carInfo = carInfoService.getById(id);
        if (carInfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(carInfo);
    }

    /**
     * 编辑
     *
     * @param carInfo
     * @return
     */
    @ApiOperation(value = "编辑", notes = "编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CarInfo carInfo) {
        carInfoService.updateById(carInfo);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping(value = "/deleteById")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        carInfoService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        carInfoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过Excel导入数据
     *
     * @param multipartFile
     * @return
     */
    @ApiOperation(value = "Excel导入数据", notes = "Excel导入数据")
    @PostMapping(value = "/importExcel")
    public Result<?> importExcel(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        ImportParams params = new ImportParams();
        params.setHeadRows(2);
        List<CarInfo> userList = ExcelImportUtil.importExcel(multipartFile.getInputStream(),
                CarInfo.class, params);
        carInfoService.saveBatch(userList);
        return Result.OK("导入成功！");
    }

    /**
     * 通过Excel导出数据
     *
     * @param ids
     * @param carInfo
     * @return
     */
    @ApiOperation(value = "Excel导出数据", notes = "Excel导出数据")
    @GetMapping(value = "/exportExcel")
    public void exportExcel(String ids, CarInfo carInfo, HttpServletResponse response) throws Exception {
        //多选按钮选择的id
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        //存在选择的id
        if (StringUtils.hasText(ids)) {
            queryWrapper.in("id", ids.split(","));
        }
        //要导出的车辆列表
        List<CarInfo> exportList = carInfoService.list(queryWrapper);
        log.info("导出Excel,共" + exportList.size() + "条数据。");
        //设置导出样式
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("车辆列表", "车辆列表"), CarInfo.class, exportList);
        //设置响应类型
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //写出，浏览器下载
        workbook.write(response.getOutputStream());
    }

}

